import { EventUpdate } from "@ui/utils/var"
import { ColComEmits, ColComProps } from "./prop"
import { SetupContext } from "vue"
import { provideFormTableId } from "../src/use-form-table"

export function use(props: ColComProps, emit: SetupContext<ColComEmits>['emit']) {
  const val = ref<any>(props.modelValue)
    , change = inject(provideFormTableId)
  watch(() => props.modelValue, (v) => {
    val.value = v
  })
  watch(() => props.type, (v) => {
    if (v === 'sequence')
      setVal(props.rowIndex)
  }, { immediate: true })
  function setVal(v: any) {
    val.value = v
    if (change)
      change(props.colId, props.rowIndex, v)
    emit(EventUpdate, v)
  }
  return { val, setVal }
}